package nbcp.web.sys

import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.RestController
import org.w3c.dom.ranges.Range
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.base.utils.Md5Util
import nbcp.comm.JsonpMapping
import nbcp.db.*
import nbcp.db.excel.ExcelComponent
import nbcp.db.mysql.*
import nbcp.db.mysql.entity.*
import nbcp.service.findByLoginName
import nbcp.web.MyHttpRequestWrapper
import java.time.LocalDateTime
import javax.servlet.http.HttpServletRequest

/**
 * Created by yuxh on 2018/9/27
 */
@Api(tags = arrayOf("人员"))
@RestController
@JsonpMapping("/user")
class UserController {
    @Value("\${shop.uploadServer.path}")
    lateinit var uploadPath: String


//    val excel by lazy {
//        return@lazy ExcelComponent("");
//    }

    @JsonpMapping("/list")
    fun list_user(name: String, mobile: String, skip: Int, take: Int): ListResult<JsonMap> {
        var data = dbr.system.s_user.query()

        if (name.HasValue) {
            data.where { it.name like "%${name}%" }
        }

        if (mobile.HasValue) {
            data.where { it.mobile like "%${mobile}%" }
        }

//        if (isChecker.HasValue) {
//            data.where { it.isChecker match_equal isChecker.AsBoolean() }
//        }
//
//        if (isManager.HasValue) {
//            data.where { it.isManager match_equal isManager.AsBoolean() }
//        }
        data.orderByDesc { it.id }
                .limit(skip, take)

        return data.toMapListResult()
    }


    @JsonpMapping("/resetPassword")
    fun forgetPassword(userId:Int): JsonResult {
        var defaultPassword= Md5Util.getBase64Md5("12345678");
        dbr.system.s_login_user.updateById(userId)
                .set { it.forget_password to false }
                .set{it.password to defaultPassword}
                .exec()

        if (dbr.affectRowCount > 0) {
            return JsonResult();
        } else return JsonResult("重置用户密码失败")
    }
}